/*
 * Copyright (C) 2014 luke.
 * 
 * This file is part of Silvie.
 * 
 * Silvie is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Silvie is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Silvie.  If not, see <http://www.gnu.org/licenses/>.
 */

package silvie.resources.images;

import java.io.File;

import utils.dernc.DeRNC;

/**
 * AbstractImage specialization for Silver level backgrounds (contained in PAK
 * files). PAK files are packed with RNC.
 * 
 * @author luke
 * @see <a href="http://segaretro.org/Rob_Northen_compression">Rob Northen
 *      compression</a>
 */
public class PAKBackgroundImage extends AbstractImage {
	private static final int PAK_WIDTH_OFFSET = 8;
	private static final int PAK_HEIGHT_OFFSET = 12;
	private static final int PAK_PALETTE_OFFSET = 52;
	private static final int PAK_PIXELS_OFFSET = 824;
	private byte[] unpackedBytes;

	/**
	 * PAKBackgroundImage constructor.
	 * 
	 * @param PAKFile the PAK file.
	 */
	public PAKBackgroundImage(File PAKFile) {
		super(PAKFile);
		super.widthOffset = PAK_WIDTH_OFFSET;
		super.heightOffset = PAK_HEIGHT_OFFSET;
		super.paletteOffset = PAK_PALETTE_OFFSET;
		super.pixelIndicesOffset = PAK_PIXELS_OFFSET;
		this.unpackedBytes = null;
	}

	@Override
	public void extract() throws Exception {
		super.extract();
		this.unpackedBytes = DeRNC.main_unpack(super.resourceBytes);
		readDimensions();
		readPalette();
		readPixelIndices();
	}

	@Override
	protected byte[] getImageBytes() {
		return this.unpackedBytes;
	}
}
